
import java.awt.BorderLayout;
import java.awt.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
//主界面
public class UI {
	//主界面
	JFrame frame;
	JTextField name_t, phone_t, grade_t;
	JLabel name_l, phone_l, grade_l;
	JRadioButton male_b, female_b;
	ButtonGroup bg;
	JPanel panel1, panel2, panel3; 
	//增加和查询的按钮
	JButton add_b, query_b;
	//创建数据库连接
	static String URL = "jdbc:mysql://localhost:3306/hw?serverTimezone=UTC ";
	static String USER = "root";
	static String PASSWORD = "zhc20040309";
	static String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	Connection conn;
	PreparedStatement pstmt;
	ResultSet rs;
	//数据库执行语句
	String sql = null;
	//表格
	Vector vdata = new Vector();
	Vector vcoldata = new Vector();
	DefaultTableModel TableModel;
	JTable Table;
	JScrollPane JSP;
	
	//构造方法同时定义排布格式
	UI(String title){
		//设置窗口大小
		frame = new JFrame(title);
		frame.setSize(800, 500);
		frame.setLayout(new BorderLayout());
		//创建展示控件并放入panel1
		name_l = new JLabel("姓名");
		phone_l = new JLabel("电话号");
		grade_l = new JLabel("成绩");
		//创建输入控件并放入panel1
		name_t = new JTextField(15);
		phone_t = new JTextField(15);
		grade_t = new JTextField(15);
		//单选框
		bg = new ButtonGroup();
		male_b = new JRadioButton("男");
		female_b = new JRadioButton("女");
		bg.add(male_b);
		bg.add(female_b);
		//添加到面板中
		panel1 = new JPanel();
		panel1.add(name_l);
		panel1.add(name_t);
		panel1.add(male_b);
		panel1.add(female_b);
		panel1.add(phone_l);
		panel1.add(phone_t);
		panel1.add(grade_l);
		panel1.add(grade_t);
		frame.add(panel1,BorderLayout.NORTH);
		//连接数据库并更新数据到表格中
		try {
			//加载数据库驱动
			Class.forName(JDBC_DRIVER);
			//创建数据库连接
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			//搜索数据库相关表单的信息
			sql = "SELECT * FROM Grades";
			//定义预处理语句
			pstmt = conn.prepareStatement(sql);
			//获取结果集合,注意这里没有参数，因为在创建的时候已经指向了sql语句
			rs = pstmt.executeQuery();
			while(rs.next()) {//一直扫描到结束,需要注意的一点是这里的标号是从1开始的！！！
				Vector vrow = new Vector();
				vrow.add(rs.getString(1));
				vrow.add(rs.getString(2));
				vrow.add(rs.getString(3));
				vrow.add(rs.getString(4));
				vdata.add(vrow);
			}
			vcoldata.add("姓名");
			vcoldata.add("性别");
			vcoldata.add("电话号");
			vcoldata.add("成绩");
			TableModel = new DefaultTableModel(vdata, vcoldata);
			Table = new JTable(TableModel);
			JSP = new JScrollPane(Table);
			//刷新表格
			panel2 = new JPanel();
			panel2.add(JSP);
			Table.updateUI();
			frame.add(panel2, BorderLayout.CENTER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//增加和查询
		panel3 = new JPanel();
		add_b = new JButton("添加");
		query_b = new JButton("查询");
		panel3.add(add_b);
		panel3.add(query_b);
		//给按钮添加事件监听器
		add_b.addActionListener(new addListener(this));
		//查询通过lamda表达式实现
		query_b.addActionListener(e->{
			//获取内容，同时进行相关操作
			String name = name_t.getText();
			String sex = null;
			for (Component c : panel1.getComponents()) {
				if(c instanceof JRadioButton) {
					sex = ((JRadioButton) c).getText();
				}
			}
			String phone = phone_t.getText();
			String grade = grade_t.getText();
			try {
				Class.forName(JDBC_DRIVER);
				conn = DriverManager.getConnection(URL, USER, PASSWORD);
				sql = "select * from Grades where name = ? and phone = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, name);
				pstmt.setString(2, phone);
				rs = pstmt.executeQuery();
				//先将数据表中内容清空
				TableModel.getDataVector().clear();
				if(!rs.next()) {
					//弹窗提示没有找到
					JOptionPane.showMessageDialog(null, "没有找到这个数据！");
				}else {
					Vector vrow = new Vector();
					vrow.add(rs.getString(1));
					vrow.add(rs.getString(2));
					vrow.add(rs.getString(3));
					vrow.add(rs.getString(4));
					vdata.add(vrow);
					System.out.print(1);
					vcoldata.add("姓名");
					vcoldata.add("性别");
					vcoldata.add("电话号");
					vcoldata.add("成绩");
					TableModel = new DefaultTableModel(vdata, vcoldata);
					//更新内容
					Table.updateUI();
					JOptionPane.showMessageDialog(null, "查询成功！");
				}
			} catch (ClassNotFoundException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				try {
					rs.close();
					pstmt.close();
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		frame.add(panel3, BorderLayout.SOUTH);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	public static void main(String args[]) {
		new UI("通讯录管理系统");
	}
	
}
